package com.qtyx.nms.ui.activity

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.qtyx.nms.R
import com.qtyx.nms.mvp.model.bean.MessageBean
import com.qtyx.nms.mvp.presenter.MessagePresenter
import com.qtyx.nms.mvp.view.MessageView
import com.qtyx.nms.ui.adapter.MessageAdapter
import com.qtyx.nms.ui.base.BaseActivity
import com.qtyx.nms.ui.base.BaseMVPActivity
import com.qtyx.nms.util.IntentKey
import kotlinx.android.synthetic.main.ac_message_list.*

/**
 * 消息列表
 * 左标题，右详情
 */
class MessageListActivity: BaseMVPActivity<MessagePresenter>(),MessageView {
    override fun getLayoutId()= R.layout.ac_message_list
    override fun createPresenter()=MessagePresenter(this)

    var list=ArrayList<MessageBean.Data>()
    var selectPosition=0
    lateinit var adapter: MessageAdapter

    override fun initData() {
        list=intent.getParcelableArrayListExtra(IntentKey.messageList)!!
        selectPosition=intent.getIntExtra(IntentKey.messageSelectPosition,-1)
        if(!list.isNullOrEmpty()){
            val selectData=list[selectPosition]
            //未读状态，调用接口更新状态
            if(0==selectData.readType){
                presenter.updateReadType(selectData.id.toString())
            }
            rvMessage.layoutManager=LinearLayoutManager(mContext)
            adapter=MessageAdapter(mContext,list)
            adapter.selectPosition=selectPosition
            rvMessage.adapter=adapter
            wvMessage.loadDataWithBaseURL(null,selectData.noticeContent, "text/html" , "UTF-8", null)

            adapter.onItemClickListener=object :MessageAdapter.OnItemClickListener{
                override fun onItemClick(position: Int, data: MessageBean.Data) {
                    selectPosition=position
                    //未读状态，调用接口更新状态
                    if(0==data.readType){
                        presenter.updateReadType(data.id.toString())
                    }
                    wvMessage.loadDataWithBaseURL(null,data.noticeContent, "text/html" , "UTF-8",null)
                }
            }

            llMessageList.visibility=View.VISIBLE
            tvMessageEmptyHint.visibility=View.GONE
        }else{
            llMessageList.visibility=View.GONE
            tvMessageEmptyHint.visibility=View.VISIBLE

        }

    }

    override fun updateStateSucceed() {
        list[selectPosition].readType=1
        adapter.notifyDataSetChanged()
    }

}